package com.cmcc.cmii.smspush.wechat.mytest;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.mp.api.WxMpMaterialService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@RequestMapping("/wechat")
public class WechatUploadTest {

    // 7GQvt5RGbF8n_andxN2aJVMW0K508Ln0oXkyFZOrR0tilyJ2O8p-TEjeXxQx_5cP
    @PostMapping("/upload")
    public String uploadFile(FilePart file) throws Exception{
        WxMpService wxMpService = new WxMpServiceImpl();

        WxMpDefaultConfigImpl wxStorage = new WxMpDefaultConfigImpl();
        wxStorage.setAppId("wxb9ed925bae5391f2");//appid
        wxStorage.setSecret("1c869cfc40f313a45b5fe096865a20e9");
        wxMpService.setWxMpConfigStorage(wxStorage);
        WxMpMaterialService materialService = wxMpService.getMaterialService();
        String filename = file.filename();
        String substring = filename.substring(filename.lastIndexOf("."));
        File file1 = new File("D:\\"+filename);
        file.transferTo(file1);
        // new File("D:\\证件照\\"+filename)
        WxMediaUploadResult jpg = materialService.mediaUpload(WxConsts.MediaFileType.IMAGE, file1);
        System.out.println(jpg);
        return "success";
    }
}
